package es.udc.emarketapp.emarket.model.client.dao;

import es.udc.emarketapp.emarket.model.client.vo.ClientVO;
import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;
import es.udc.fbellas.j2ee.util.sql.GeneralOperations;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Calendar;

public class MySQLClientDAO extends AbstractSQLClientDAO {

    public ClientVO create(Connection connection, ClientVO clientVO)
            throws InternalErrorException {

        PreparedStatement preparedStatement = null;

        try {

            //Creamos el statement
            String query = "INSERT INTO usuario (id_usuario, pass, f_alta, "
                    + "ultima_act, aprobado, valido, nom_pila, ap1, ap2, nif_cif, dir, "
                    + "ciudad, cp, prov, pais, email, tlf1, tlf2, tipo_usuario) "
                    + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

            preparedStatement = connection.prepareStatement(query);

            //Ahora lo llenamos con los parametros que se quiere
            int i = 1;
            preparedStatement.setString(i++, clientVO.getIdComprador());
            preparedStatement.setString(i++, clientVO.getPass());
            preparedStatement.setTimestamp(i++,
                    new Timestamp(Calendar.getInstance().getTime().getTime()));
            preparedStatement.setTimestamp(i++,
                    new Timestamp(Calendar.getInstance().getTime().getTime()));
            preparedStatement.setBoolean(i++, clientVO.isAprobado());
            preparedStatement.setBoolean(i++, clientVO.isValido());
            preparedStatement.setString(i++, clientVO.getNomPila());
            preparedStatement.setString(i++, clientVO.getAp1());
            preparedStatement.setString(i++, clientVO.getAp2());
            preparedStatement.setString(i++, clientVO.getNifCif());
            preparedStatement.setString(i++, clientVO.getDir());
            preparedStatement.setString(i++, clientVO.getCiudad());
            preparedStatement.setString(i++, clientVO.getCp());
            preparedStatement.setString(i++, clientVO.getProv());
            preparedStatement.setString(i++, clientVO.getPais());
            preparedStatement.setString(i++, clientVO.getEmail());
            preparedStatement.setString(i++, clientVO.getTlf1());
            preparedStatement.setString(i++, clientVO.getTlf2());
            preparedStatement.setString(i++, clientVO.getTipoUsuario());


            // Execute query.
            int insertedRows = preparedStatement.executeUpdate();

            if (insertedRows == 0) {
                throw new SQLException("No se pudo insertar la fila a la tabla"
                        + " 'Usuario'");
            }

            if (insertedRows > 1) {
                throw new SQLException("Fila duplicada en la tabla 'Usuario'");
            }


            //*************************

            //Creamos el statement
            query = "INSERT INTO comprador (id_comprador, f_nac, "
                    + "dir_envio, ciudad_envio, cp_envio, prov_envio, pais_envio) "
                    + "VALUES (?, ?, ?, ?, ?, ?, ?)";

            preparedStatement = connection.prepareStatement(query);

            //Ahora lo llenamos con los parametros que se quiere
            i = 1;
            preparedStatement.setString(i++, clientVO.getIdComprador());
            preparedStatement.setTimestamp(i++,
                    new Timestamp(clientVO.getFNac().getTimeInMillis()));
            preparedStatement.setString(i++, clientVO.getDirEnvio());
            preparedStatement.setString(i++, clientVO.getCiudadEnvio());
            preparedStatement.setString(i++, clientVO.getCpEnvio());
            preparedStatement.setString(i++, clientVO.getProvEnvio());
            preparedStatement.setString(i++, clientVO.getPaisEnvio());

            // Execute query.
            insertedRows = preparedStatement.executeUpdate();

            if (insertedRows == 0) {
                throw new SQLException("No se pudo insertar la fila a la tabla"
                        + " 'Comprador'");
            }

            if (insertedRows > 1) {
                throw new SQLException("Fila duplicada en la tabla 'Comprador'");
            }


            return clientVO;

        } catch (SQLException e) {
            throw new InternalErrorException(e);
        } finally {
            GeneralOperations.closeStatement(preparedStatement);
        }

    }


}
